import {defineStore} from "pinia";
import zjRequest from "@/services";
import {IActions, IState} from "@/store/goodsModule/types";
import useMyModule from "@/store/myModule";

const useGoodsModule = defineStore<any, IState, any, IActions>("goodsModule", {
    state: () => ({
        classificationList: [],
        goodsList: [],
        goodsListPageInfo: {
            pageSize: 6,
            pageNum: 1,
            pageTotal: 0
        },
        // 搜索的商品
        searchGoodsList: [],
        searchGoodsPageInfo: {
            pageSize: 6,
            pageNum: 1,
            pageTotal: 0
        },
        hotSearchList: [], // 热搜
        recommendSearchList: [], // 搜索发现
        currentGoodsDetail: {}, // 当前详情
        currentGoodsSpec: {
            attributeKeyVos: [],
            sysProductSpecsVoList: []
        },// 规格数据
        currentGoodsStock: 0 // 总库存
    }),
    actions: {
        // 通过类别id获取产品，可分页
        async getGoodsByClassifyAction(params) {
            const {classifyId, isMoreMode, ...otherParams} = params
            const res = await zjRequest.get({
                url: "/product/" + classifyId,
                params: otherParams
            })
            if (res.code === 0) {
                return res.data
            } else {
                return null
            }
        },
        // 设置商品
        async setGoodsClassifyAction(params) {
            const data = await this.getGoodsByClassifyAction(params)
            if (data) {
                if (params.isMoreMode) {
                    this.goodsList = [...this.goodsList, ...data.list]
                } else {
                    this.goodsList = data.list
                }
                this.goodsListPageInfo = {
                    pageSize: data.pageSize,
                    pageNum: data.pageNum,
                    pageTotal: data.pageTotal,
                }
            }
            return !!data
        },
        // 获取分类
        async getClassificationAction() {
            const res = await zjRequest.get({
                url: "/category"
            })
            if (res.code === 0) {
                return res.data.filter((item: any) => item.name !== "轮播广告")
            } else {
                return null
            }
        },
        // 设置分类
        async setClassificationAction() {
            const data = await this.getClassificationAction()
            if (data) {
                this.classificationList = data
            }
            return !!data
        },
        // 模糊搜索全部商品
        async getSearchGoodsAction(params) {
            const myModule = useMyModule()
            let requestUrl = "/product/1"
            const {isMoreMode, categoryId, ...otherParams} = params
            if (myModule.userInfo.id) {
                otherParams.userId = myModule.userInfo.id
            }
            if (!params.isGlobalSearch) {
                requestUrl = `/product/${categoryId}`
            }
            const res = await zjRequest.get({
                url: requestUrl,
                params: otherParams
            })
            if (res.code === 0) {
                return res.data
            } else {
                return null
            }
        },
        // 设置模糊搜索的商品
        async setSearchGoodsAction(params) {
            const data = await this.getSearchGoodsAction(params)
            if (data) {
                if (params.isMoreMode) {
                    this.searchGoodsList = [...this.searchGoodsList, ...data.list]
                } else {
                    this.searchGoodsList = data.list
                }
                this.searchGoodsPageInfo = {
                    pageSize: data.pageSize,
                    pageNum: data.pageNum,
                    pageTotal: data.pageTotal
                }
            }
            return !!data
        },
        // 热门搜索
        async getHotSearchAction() {
            const res = await zjRequest.get({
                url: "/product/searchHotWord"
            })
            if (res.code === 0) {
                return res.data.slice(0, 4)
            } else {
                return null
            }
        },
        // 设置热搜数据
        async setHotSearchAction() {
            const data = await this.getHotSearchAction()
            if (data) {
                this.hotSearchList = data
            }
            return !!data
        },
        async getRecommendSearch() {
            const myModule = useMyModule()
            let userId = 0
            if (!myModule.userInfo.id) {
                return null
            }
            userId = myModule.userInfo.id
            const url = userId ? `/product/searchUserHotWord/${userId}` : "/product/searchUserHotWord"
            const res = await zjRequest.get({
                url,
            })
            if (res.code === 0) {
                return res.data
            } else {
                return null
            }
        },
        async setRecommendSearch() {
            const data = await this.getRecommendSearch()
            if (data) {
                this.recommendSearchList = data.map((item: any) => item.value)
            }
            return !!data
        },
        // 商品详情
        async getGoodsDetailAction(params) {
            const {goodsId, ...otherParams} = params
            const res = await zjRequest.get({
                url: `/product/detail/${goodsId}`,
                params: otherParams,
                needToast: false
            })
            if (res.code === 0) {
                return res.data
            } else {
                return null
            }
        },
        // 设置当前的商品详情
        async setGoodsDetailAction(params) {
            const data = await this.getGoodsDetailAction(params)
            if (data) {
                this.currentGoodsDetail = data
            }
            return !!data
        },
        // 规格获取
        async getGoodsSpecAction(params) {
            const {goodsId, categoryId} = params
            if (!goodsId || !categoryId) return
            const res = await zjRequest.get({
                url: `/product/specs/${categoryId}/${goodsId}`,
            })
            if (res.code === 0) {
                return res.data
            } else {
                return null
            }
        },
        // 商品详情规格设置
        async setGoodsSpecAction(params) {
            const data = await this.getGoodsSpecAction(params)
            if (data) {
                this.currentGoodsSpec = data
                this.currentGoodsStock = this.currentGoodsSpec?.sysProductSpecsVoList?.reduce((prev, item) => {
                    prev += item.productStock
                    return prev
                }, 0)
            }
            return !!data
        }
    }
})
export default useGoodsModule
